<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <script src="FileSaver.js" charset="utf-8"></script>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <!-- 引入组件库 -->
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
</head>

<body>
    <div id="app">
        <el-container>
            <el-header>何柳熠的课程设计</el-header>
            <el-container>
                <el-aside width="200px">
                    <el-upload ref="upload" action="alert" accept=".dat" :auto-upload="false" :file-list="uploadFiles"
                        :on-change="loadJsonFromFile" multiple="false">
                        <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
                    </el-upload>
                    <el-button type="primary" :disabled="disa" @click="calculation">开始计算</el-button>
                    <el-button type="danger" :disabled="dow" round @click="down">下载in2</el-button>
                </el-aside>
                <el-container>
                    <el-main>
                        <label for="username">先验精度</label>
                        <input type="text" v-model="fwjwc" key="1" placeholder="请输入方位角误差">
                        <input type="text" v-model="a" key="1" placeholder="请输入固定误差">
                        <input type="text" v-model="b" key="1" placeholder="请输入比例误差">
                        <label for="username">起始点点号</label>
                        <input type="text" v-model="point" key="1" placeholder="请输入起点点号">
                        <label for="email">起始点坐标</label>
                        <input type="text" v-model="X" placeholder="请输入X坐标">
                        <input type="text" v-model="Y" placeholder="请输入Y坐标">
                        <br>
                        <label for="email">终点点号</label>
                        <input type="text" v-model="endPoint" placeholder="请输入终点点号">
                        <label for="email">起算方位角</label>
                        <input type="text" v-model="fwj" placeholder="请输入起算方位角">
                    </el-main>
                    <el-footer>Footer</el-footer>
                </el-container>
            </el-container>

        </el-container>
        <!-- <label for="username">先验精度</label>
        <input type="text" id="fwjwc" key="1">
        <input type="text" id="a" key="1">
        <input type="text" id="b" key="1">
        <br>
        <label for="username">起始点点号</label>
        <input type="text" id="username" key="1">
        <label for="email">起始点坐标</label>
        <input type="text" id="X">
        <input type="text" id="Y">
        <label for="email">终点点号</label>
        <input type="text" id="endPoint">
        <label for="email">起算方位角</label>
        <input type="text" id="fwj"> -->
        <!-- <button @click="read"  type="file" id="files" ref="refFile">读入文件</button> -->
        <!-- <br>
        <el-upload style="width: 25%;" ref="upload" action="alert" accept=".dat" :auto-upload="false"
            :file-list="uploadFiles" :on-change="loadJsonFromFile" multiple="false">
            <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
        </el-upload>
        <el-button type="primary" :disabled="disa" @click="calculation">开始计算</el-button> -->
    </div>
    <style>
        .el-row {
            margin-bottom: 20px;

            &:last-child {
                margin-bottom: 0;
            }
        }

        .el-col {
            border-radius: 4px;
        }

        .bg-purple-dark {
            background: #99a9bf;
        }

        .bg-purple {
            background: #d3dce6;
        }

        .bg-purple-light {
            background: #e5e9f2;
        }

        .grid-content {
            border-radius: 4px;
            min-height: 50px;
            font-size: 20px;
            text-align: center;
        }

        .row-bg {
            padding: 10px 0;
            background-color: #f9fafc;
        }

        .el-header,
        .el-footer {
            background-color: #B3C0D1;
            color: #333;
            text-align: center;
            line-height: 60px;
        }

        .el-aside {
            background-color: #D3DCE6;
            color: #333;
            text-align: center;
            line-height: 200px;
        }

        .el-main {
            background-color: #E9EEF3;
            color: #333;
            text-align: center;
            line-height: 160px;
        }

        body>.el-container {
            margin-bottom: 40px;
        }

        .el-container:nth-child(5) .el-aside,
        .el-container:nth-child(6) .el-aside {
            line-height: 260px;
        }

        .el-container:nth-child(7) .el-aside {
            line-height: 100%;
        }
    </style>
    <script type="text/javascript">
        var app = new Vue({
            el: "#app",
            data: {
                disa: true,
                fwjwc: '',
                a: '',
                b: '',
                point: '',
                X: '',
                Y: '',
                endPoint: '',
                fwj: '',
                table: [],
                dltx: [],
                dlty: [],
                stationName: 'C04-QZ2',
                uploadFiles: '',
                result: '',
                dow: true,
            },
            methods: {
                loadJsonFromFile(file, fileList) {
                    var that = this;
                    this.uploadFiles = fileList
                    console.log(111)
                    console.log(this.uploadFiles)
                    const selectedFile = this.fileList;
                    let reader = new FileReader()
                    reader.readAsText(file.raw)
                    reader.onload = (e) => {
                        // 读取文件内容
                        const fileString = e.target.result
                        var strs = new Array(); //定义一数组
                        strs = fileString.split("\r\n"); //字符分割
                        strs.forEach(element => {
                            if (element != "") {
                                this.table.push((element.split(',')).slice(1))
                            }
                        });
                        that.disa = false;
                        console.log(this.table)
                        this.$message({
                            message: '文件读取成功',
                            type: 'success'
                        });
                        // console.log(this.getCol(this.table,2))
                    }
                },
                calculation() {
                    var that = this;
                    var els = document.getElementsByTagName("input");
                    console.log(els)
                    for (var i = 0, j = els.length - 1; i < j; i++) {
                        if (els[i].value == "" || els[i].value == undefined || els[i].value == null) {
                            that.$alert(els[i].placeholder);
                            return false;
                        }
                    }
                    var result = ''
                    var F = true
                    result+=this.fwjwc+','+this.a+','+this.b+',\n'
                    result+=this.point+','+this.X+','+this.Y+',\n'
                    for (var i = 0; i < this.table.length; i++) {
                        // A,B=this.getOne(this.table[i])
                        A = this.getOne(this.table[i])
                        result+=this.table[i][0]+'\n'
                        console.log(A[0])
                        console.log(A[1])
                        // for(var j =0;j<A[0].length;j++){
                        //     if(A[0][i]==this.endPoint && F){
                        //         str=A[0][j]+",A"+this.fwj+'\n'
                        //     }
                        //     if(A[0][j]==this.table[i][0]){
                        //         continue
                        //     }
                        //     else{
                        //         str=
                        //     }A[0]距离观测值
                        for (var j = 0; j < this.table.length; j++) {
                            if (this.table[j][0] == this.endPoint && F) {
                                str = this.table[j][0] + ",A," + this.fwj + '\n'
                                result += str
                                F = false
                            }
                            if (this.table[j][0] == this.table[i][0]) {
                                continue
                            }
                            else {
                                str = this.table[j][0] + ",L," + A[1][j] + "\n"
                                result += str
                            }
                        }
                        for (var j = 0; j < this.table.length; j++) {
                            if (this.table[j][0] == this.table[i][0]) {
                                continue
                            }
                            else {
                                str = this.table[j][0] + ",S," + A[0][j] + "\n"
                                result += str
                            }
                        }

                    }
                    this.result = result
                    this.dow =false
                    console.log(result)
                    this.$message({
                        message: '计算完成',
                        type: 'success'
                    });
                    // this.disa=true
                    // this.getOne(this.table[0]);
                    // console.log('开始计算')
                },
                down() {
                    var data = this.result
                    let str = new Blob([data], { type: 'text/plain;charset=utf-8' });
                    // 注意这里要手动写上文件的后缀名
                    saveAs(str, `导出文件时的名字.in2`);
                },
                getOne(CZ) {
                    Atan = new Array();
                    azimuthRAD = new Array();
                    deg = new Array();
                    FXJ = new Array();
                    Obs = new Array();
                    distance = new Array();
                    disObs = new Array();
                    this.dlty = []
                    this.dltx = []
                    this.table.forEach(element => {
                        if (element[0] == CZ[0]) {
                            this.dltx.push(NaN)
                            this.dlty.push(NaN)
                        }
                        else {
                            this.dlty.push(parseFloat(element[1] - parseFloat(CZ[1])))
                            this.dltx.push(parseFloat(element[2] - parseFloat(CZ[2])))
                        }
                    });
                    console.log("dlt" + this.dltx)
                    for (j = 0; j < this.dltx.length; j++) {
                        Atan.push(Math.atan(this.dlty[j] / this.dltx[j]));
                    }
                    // console.log(Atan)
                    for (j = 0; j < Atan.length; j++) {
                        if (this.dlty[j] > 0 && this.dltx[j] < 0) { azimuthRAD.push(Math.PI + Atan[j]); continue; }
                        if (this.dlty[j] < 0 && this.dltx[j] < 0) { azimuthRAD.push(Math.PI + Atan[j]); continue; }
                        if (this.dlty[j] < 0 && this.dltx[j] > 0) { azimuthRAD.push(Math.PI * 2 + Atan[j]); continue; }
                        else azimuthRAD.push(Atan[j]);
                    }
                    // console.log("方位角" + azimuthRAD)
                    azimuthRAD.forEach(element => {
                        deg.push(element * 180 / Math.PI);

                    });
                    // console.log("观测角" + deg)
                    if (isNaN(deg[0])) {
                        initAngle = deg[1];
                    }
                    else {
                        initAngle = deg[0];
                    }
                    console.log("init" + initAngle)
                    for (i = 0; i < deg.length; i++) {
                        if (deg[i] != NaN) {
                            c = deg[i] - initAngle;
                            if (c < 0) {
                                // console.log("c" + c)
                                c += 360;
                            }
                            FXJ.push(c);
                        }
                        else {
                            FXJ.push(NaN);
                        }
                    }
                    // console.log("FXJ" + FXJ)//fxj

                    ra = 0.0;
                    for (i = 0; i < FXJ.length; i++) {
                        if (isNaN(FXJ[i])) {
                            Obs.push(NaN);
                        }
                        else if (FXJ[i] == 0.0) {
                            Obs.push(FXJ[i]);
                        }
                        else {
                            ra = this.normalRandom(0, this.fwjwc);
                            Obs.push(FXJ[i] + ra / 3600);  //模拟观测值  方向角+随机值
                        }
                    }
                    console.log("obs" + Obs)

                    for (j = 0; j < this.dltx.length; j++) {
                        distance.push(Math.pow((Math.pow(this.dltx[j], 2) + Math.pow(this.dlty[j], 2)), 0.5));
                    }
                    console.log(distance)//

                    for (i = 0; i < distance.length; i++) {
                        if (isNaN(distance[i])) {
                            disObs.push(distance[i]);
                        }
                        else {
                            ra = this.normalRandom(0, parseFloat(this.a) + parseFloat(this.b) * distance[i] / 1000);
                            disObs.push(distance[i] + ra / 1000);  //模拟观测值  方向角+随机值
                        }
                    }
                    return [disObs, Obs]
                    console.log("disobs" + disObs)
                },
                getCol(matrix, col) {
                    var column = [];
                    for (var i = 0; i < matrix.length; i++) {
                        column.push(matrix[i][col]);
                    }
                    return column;
                },
                normalRandom(mean, std) {
                    let u = 0.0, v = 0.0, w = 0.0, c = 0.0;
                    do {
                        //获得两个（-1,1）的独立随机变量
                        u = Math.random() * 2 - 1.0;
                        v = Math.random() * 2 - 1.0;
                        w = u * u + v * v;
                    } while (w == 0.0 || w >= 1.0)
                    //Box-Muller转换
                    c = Math.sqrt((-2 * Math.log(w)) / w);
                    let normal = mean + (u * c) * std;
                    return normal;
                }
            }
        })

    </script>
</body>

</html>